package logging

import (
	"fmt"
	"log"
	"os"
	"path"
	"time"
)

// getLogFilePath get the log file save path
func getLogFilePath() string {
	//return fmt.Sprintf("%s%s%s", setting.Conf.App.RuntimeRootPath, setting.Conf.App.LogSavePath, getMonthLogSavePath())
	return ""
}

// getLogFileName get the save name of the log file
func getLogFileName(level string) string {
	/*	return fmt.Sprintf("%s_%s.%s",
		level,
		time.Now().Format(setting.Conf.App.TimeFormat),
		setting.Conf.App.LogFileExt,
	)*/
	return ""
}

// getMonthLogSavePath 獲取按年月的文件夾路徑
func getMonthLogSavePath() string {
	return fmt.Sprintf("%s",
		time.Now().Format("2006/01/"),
	)
}

// IsNotExistMkDir create a directory if it does not exist
func IsNotExistMkDir(src string) error {
	_, err := os.Stat(src)
	if notExist := os.IsNotExist(err); notExist == true {
		if err := os.MkdirAll(src, os.ModePerm); err != nil {
			return err
		}
	}
	return nil
}

func MustOpen(dir, filePath, fileName string) (F *os.File, err error) {
	if dir == "" {
		dir, _ = os.Getwd()
	}
	src := path.Join(dir, filePath)
	err = IsNotExistMkDir(src)
	if err != nil {
		log.Printf("E! file.IsNotExistMkDir src: %s, err: %v", src, err)
		return
	}
	path := path.Join(dir, filePath, fileName)
	F, err = os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
	return
}
